package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.UploadFileForm;
import com.feeyin.radar.crm.service.AssignCustomerService;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.ExceptionCustomerService;
import com.feeyin.radar.crm.service.OpenCustomerService;
import com.feeyin.radar.sys.service.AssessmentTypeService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class AssignCustomerController extends MultiActionController
{
  private OpenCustomerService openCustomerService;
  private AssignCustomerService assignCustomerService;
  private CustomerService customerService;
  private ExceptionCustomerService exceptionCustomerService;
  private AssessmentTypeService assessmentTypeService;
  private String assignCustomer;
  private String assignBatchImport;
  private String assignBatchImportLinkman;

  public OpenCustomerService getOpenCustomerService()
  {
    return this.openCustomerService;
  }

  public void setOpenCustomerService(OpenCustomerService openCustomerService) {
    this.openCustomerService = openCustomerService;
  }

  public AssignCustomerService getAssignCustomerService() {
    return this.assignCustomerService;
  }

  public void setAssignCustomerService(AssignCustomerService assignCustomerService) {
    this.assignCustomerService = assignCustomerService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public ExceptionCustomerService getExceptionCustomerService() {
    return this.exceptionCustomerService;
  }

  public void setExceptionCustomerService(ExceptionCustomerService exceptionCustomerService)
  {
    this.exceptionCustomerService = exceptionCustomerService;
  }

  public AssessmentTypeService getAssessmentTypeService() {
    return this.assessmentTypeService;
  }

  public void setAssessmentTypeService(AssessmentTypeService assessmentTypeService) {
    this.assessmentTypeService = assessmentTypeService;
  }

  public String getAssignCustomer() {
    return this.assignCustomer;
  }

  public void setAssignCustomer(String assignCustomer) {
    this.assignCustomer = assignCustomer;
  }

  public String getAssignBatchImport() {
    return this.assignBatchImport;
  }

  public void setAssignBatchImport(String assignBatchImport) {
    this.assignBatchImport = assignBatchImport;
  }

  public String getAssignBatchImportLinkman() {
    return this.assignBatchImportLinkman;
  }

  public void setAssignBatchImportLinkman(String assignBatchImportLinkman) {
    this.assignBatchImportLinkman = assignBatchImportLinkman;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String customerName = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    map.put("sFlag", sFlag);
    map.put("customerName", customerName);

    map.put("ranges", this.customerService.getCustomerDictionaryRanges());

    Page page = new Page();
    page.getPageList(this.assignCustomerService.getPageAssignCustomersCount(customerName, userId).intValue(), null, request);
    map.put("customers", this.assignCustomerService.getPageAssignCustomers(customerName, userId, page));

    if (this.assessmentTypeService.isExpUser(userId)) {
      map.put("isExpUser", "1");
    }
    map.put("customerLimitForm", this.customerService.getCustomerLimit(userId));

    return new ModelAndView(getAssignCustomer(), map);
  }

  public ModelAndView salePersonToCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    String salePersonId = request.getParameter("salePersonId");
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    if (!StringUtils.isEmpty(salePersonId)) {
      result = this.exceptionCustomerService.salePersonToCustomer(ids, Integer.valueOf(Integer.parseInt(salePersonId)));
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./assignCustomer.do?action=display&result=" + success));
  }

  public ModelAndView moveCustomerToTarget(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    boolean result = false;
    int success = 0;

    String isTarget = request.getParameter("isTarget");
    String[] ids = request.getParameterValues("id");
    result = this.exceptionCustomerService.moveCustomerToTarget(ids, userId, isTarget);
    if (result) success = 2;

    return new ModelAndView(new RedirectView("./assignCustomer.do?action=display&result=" + success));
  }

  public ModelAndView preAssignBatchImport(HttpServletRequest request, HttpServletResponse response)
  {
    return new ModelAndView(getAssignBatchImport());
  }

  public ModelAndView assignBatchImport(HttpServletRequest request, HttpServletResponse response, UploadFileForm uploadFileForm)
  {
    Map map = new HashMap();

    MultipartFile multipartFile = uploadFileForm.getContents();
    if (multipartFile == null) {
      request.setAttribute("result", Integer.valueOf(2));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return preAssignBatchImport(request, response);
    }
    if (multipartFile.getSize() > 10000000L) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return preAssignBatchImport(request, response);
    }

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    List logs = new ArrayList();
    String realPath = getServletContext().getRealPath("/");
    logs = this.openCustomerService.batchImportCustomer(userId, uploadFileForm, realPath, multipartFile, 1);
    map.put("logs", logs);

    return new ModelAndView(getAssignBatchImport(), map);
  }
}